"use strict";
//index.js
//获取应用实例
var app = getApp()
var common = require('../../../utils/api.js')
var api = common.api
var util = require('../../../utils/util.js')
var toast = require('../../../utils/toast.js')

Page({
  data: {
    motto: 'Hello World',
    title: '加入项目',
    userInfo: {},
    manageslist:[],
    teamleaderslist:[],
    scrollViewHeight: 200,
    productList:[],
    product:{},
    isnew:true,
    teamlist:{},
    teamselect:'',
    pid:''
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onShow: function (options) {
    
  },
  onLoad:function(options){
    util.showLoading({title: '加载中'})
    this.toast = toast
    console.log('onLoad')
    var res = wx.getSystemInfoSync()
    var that = this
    var pid = this.data.pid = options.pid || 28;
    console.log(this.data.pid)
    this.setData({scrollViewHeight:res.windowHeight*2-64-64})
    app.getUserInfo(function(){
      var openid = app.globalData.wxuserInfo.openid
      api.getuserinfo(openid,function(data){
      var userinfo = data.data
      app.globalData.userInfo = userinfo
      console.log('获取用户权限',data,userinfo)
      api.getProductInfoWithTeamlist(pid,function(result){
            console.log('getProductInfo',result,that.productstatus,that.headTitle)
            that.setData({product:result.data,teamlist:result.teamlist})
            util.hideLoading()
          })
        if(userinfo && userinfo.openid && userinfo.name && userinfo.team){
          
          that.setData({
            isnew: false
          })
        }else{
          util.hideLoading()
        }
      })
    });
  },
  bindTeamlistChange:function(e){
    var index = e.detail.value
    var name = this.data.teamlist[index].name
    console.log("选择组别",name,index)
    this.setData({
      teamselect: name
    })
  },
  bindFormSubmit:function(e){
    var uid = app.globalData.userInfo && app.globalData.userInfo.id || null;
    var formdata = e.detail.value
    var toasttitle = ""
    var that = this
    if(this.data.isnew){
      if(!formdata.name){
        toasttitle="名字未填写"
      }
      if(!formdata.team){
        toasttitle="组别未选择"
      }
    }else{
      console.log(app.globalData.userInfo)
      formdata.name = app.globalData.userInfo.name
      formdata.team = app.globalData.userInfo.team
    }
    if(uid){
      toasttitle = false
    }
    var team = !util.isEmpty(app.globalData.userInfo) ? app.globalData.userInfo.team :formdata.team
    if(toasttitle){
      this.toast(toasttitle)
    }else{
      var nickname = app.globalData.wxuserInfo.nickname || app.globalData.wxuserInfo.nickName
      var data
      if(uid){
        data = null
      }else{
        var data = {
          name:formdata.name,
          team:this.data.teamlist[team].teamindex,
          nickname:nickname,
          openid:app.globalData.wxuserInfo.openid
        }
      }
      //调用创建项目的接口
      api.joinproduct(this.data.pid,uid,data,function(result){
        console.log(data,result)
        //更新数据
        if(result.errno==0){
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000,
            complete:function(){
              wx.switchTab({
                url: '/pages/index/index/index'
              })
            }
          })
        }else{
          that.toast(result.errmsg)
        }
      })
    }
  },
  upper: function(e) {
    console.log(e)
  },
  lower: function(e) {
    console.log(e)
  },
  scroll: function(e) {
    console.log(e)
  }
})
